<template>
  <a-spin :spinning="confirmLoading">
    <a-row class="top-row">
      <a-col :lg="3" :md="12" :sm="21">
        <a-form-item :colon="false" label="采购名称">
          {{ model.name }}
        </a-form-item>
      </a-col>
      <a-col :lg="3" :md="12" :sm="21">
        <a-form-item :colon="false" label="采购单号">
          {{ model.purchaseNub }}
        </a-form-item>
      </a-col>
      <a-col :lg="3" :md="12" :sm="21">
        <a-form-item :colon="false" label="采购部门">
          {{ model.procurementDepartment_dictText }}
        </a-form-item>
      </a-col>
      <a-col :lg="3" :md="12" :sm="21">
        <a-form-item :colon="false" label="采购种类">
          {{ model.typeNub }}
        </a-form-item>
      </a-col>
      <a-col :lg="3" :md="12" :sm="21">
        <a-form-item :colon="false" label="采购金额（元）">
          {{ model.money }}
        </a-form-item>
      </a-col>
      <a-col :lg="3" :md="12" :sm="21">
        <a-form-item :colon="false" label="提报人">
          {{ model.createBy_dictText }}
        </a-form-item>
      </a-col>
      <a-col :lg="3" :md="12" :sm="21">
        <a-form-item :colon="false" label="提报时间">
          {{ model.subTime }}
        </a-form-item>
      </a-col>
    </a-row>
    <a-form-model-item label="采购附件">
      <j-upload v-model="model.attachment" :disabled="true"></j-upload>
    </a-form-model-item>

    <a-table
      :loading="loading"
      class="bpbj"
      rowKey="id"
      :columns="bpbjColumns"
      :dataSource="bpbjDataSource"
      :pagination="false"
    >
      <template slot="benci" slot-scope="text, record">

          <a-input-number @change="changeBenci(recordm,index,$event)" v-model="record.benci" :precision="0"  :min="0" :max="record.noInNub" ></a-input-number>

      </template>
      <template slot="kuwei" slot-scope="text, record, index">

          <a-input @change="changeWH(record,index,$event)"  allow-clear  @click="selectWH(record,index)" v-model="record.kuwei"  class="ruku-input">
            <a-icon v-if="record.kuwei==null||record.kuwei==''" :precision="0" :step="1" style="font-size: 24px" slot="suffix" type="ellipsis" ></a-icon>
          </a-input>
          <p style="color:red;position: absolute">{{record.errorText}}</p>


      </template>
      <template slot="name" slot-scope="text, record">

      </template>
    </a-table>

    <a-form-item :colon="false" label="到货入库说明">
      <a-textarea v-model="model.delivery"></a-textarea>
    </a-form-item>
    <SelectWarehouseModal @back="getSelectRow" ref="swhmRef"></SelectWarehouseModal>
  </a-spin>
</template>

<script>
import {getAction, httpAction, postAction} from "@api/manage";
import SelectWarehouseModal from "@views/warehouse/modules/SelectWarehouseModal";

const bpbjColumns = [
  {
    title: '备品备件名称',
    align: "center",
    dataIndex: 'name'
  },
/*  {
    title: '备品备件编号',
    align: "center",
    dataIndex: 'number'
  },*/
  {
    title: '规格型号',
    align: "center",
    dataIndex: 'model'
  },
  {
    title: '单位',
    align: "center",
    dataIndex: 'unit_dictText'
  },
  {
    title: '采购数量',
    align: "center",
    dataIndex: 'quantity',

  },
  {
    title: '未入库数量',
    align: "center",
    dataIndex: 'noInNub',

  },
  {
    title: '本次入库数量',
    align: "center",
    dataIndex: 'benci',
    scopedSlots: { customRender: 'benci' }
  },
  {
    title: '入库库位',
    align: "center",
    dataIndex: 'kuwei',
    scopedSlots: { customRender: 'kuwei' },
    width:120
  },
  // {
  //   title: '采购单价（元）',
  //   align: "center",
  //   dataIndex: 'price',
  //
  // },
]
export default {
  name: "EneArrivalStorageForm",
  components: {
    SelectWarehouseModal
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {

    return {
      rules:{
        kuwei:[{required: true, validator: this.validateKuwei, trigger: 'change' }],
        benci:[{required: true, validator: this.validateBenci, trigger: 'change' }]
      },
      loading:false,
      kuwei:'',
      shenpiyijian:"",
      bpbjColumns,
      bpbjDataSource: [],
      bpbjHeji: 0,
      model: {},

      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,

      url: {
        list: '/ene/enePurchasingManagement/queryByMainIdV2',
        add:'/ene/enePurchasingManagement/arrival'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {},
  mounted() {},
  methods: {
    changeBenci(record,index,e){
      if(record.benci<=0){
        this.bpbjDataSource[index].errorText=""
      }
    },
    changeWH(record,index,e){
      if((record.kuwei==""||record.kuwei==null)&&record.benci>0){
        this.bpbjDataSource[index].errorText="请选择库位!"
      }else{
        this.bpbjDataSource[index].errorText=""
      }
    },
    getSelectRow(value){
      this.bpbjDataSource[this.kuwei]=Object.assign(this.bpbjDataSource[this.kuwei],{'kuwei':`${value.prefix}-${value.shelfNub}-${value.layersNub}-${value.positionNub}`,'warehouseLocationId':value.id})
      if((value!=""&&value!=null)||this.bpbjDataSource[this.kuwei].benci<=0){
        this.bpbjDataSource[this.kuwei].errorText=""
      }
    },
  selectWH(record,index){
      console.log(index)
    this.$refs.swhmRef.visible=true
    this.kuwei=index

  },
    handleOk(){
      let eneArrivalStorageList =[]
      let yes = false
      let canSubmit=true
      for (let i = 0; i < this.bpbjDataSource.length; i++) {
        if (this.bpbjDataSource[i].benci>0&&(this.bpbjDataSource[i].kuwei==null||this.bpbjDataSource[i].kuwei=="")){
          this.bpbjDataSource[i].errorText="请选择库位!"
          canSubmit=false
        }else{
          this.bpbjDataSource[i].errorText=""
        }
        if (this.bpbjDataSource[i].benci==0){
          continue
        }
        yes=true
        let eneArrivalStorage = {}
        eneArrivalStorage.purchasingSparepartsId = this.bpbjDataSource[i].id
        eneArrivalStorage.nub=this.bpbjDataSource[i].benci
        //库位管理ID 没取到

        eneArrivalStorage.warehouseLocationId=this.bpbjDataSource[i].warehouseLocationId
        eneArrivalStorage.location=this.bpbjDataSource[i].kuwei
        eneArrivalStorage.illustrate=this.model.delivery
        eneArrivalStorageList.push(eneArrivalStorage)
      }
      this.model.eneArrivalStorageList=eneArrivalStorageList
      console.log("库位管理",this.model)
      //这里判断没有输入 关闭弹窗
      if(!yes||!canSubmit){
        this.$message.warning("请填写入库单")
      }else{
        this.loding=true
        //loding弹窗关闭啥的
        postAction(this.url.add,this.model).then((res)=>{

          this.$emit('cancel')
        }).finally(()=>{ this.loding=false})
      }


    },
    check(record) {
      this.model = Object.assign({}, record)
      this.visible = true
      this.loading=true
      getAction(this.url.list, {id: record.id}).then(res => {
        this.bpbjDataSource = res.result.map(e=>(Object.assign(e,{benci:0,kuwei:null,errorText:""})))
      }).finally(()=>{
        this.loading=false
      })
    },
    popupCallback(...e) {
      //e[2][0].location = e[1].location
      //console.log(this.model)
      console.log(e);
      console.log(this.bpbjDataSource)
    }
  }
}
</script>

<style scoped>
.top-row {
  background: #f4f5f7;
  padding: 12px 24px;
}

.top-row /deep/ .ant-form-item {
  margin-bottom: 0;
  color: #6b778c;
}

.top-row /deep/ .ant-form-item-label {
  line-height: 1;
}

.top-row /deep/ .ant-form-item-control {
  line-height: 1;
  margin-top: 10px;
}

.top-row /deep/ .ant-form-item-label > label {
  color: #6B778C
}
.ruku-input /deep/ .ant-input{
  background: #F4F5F7;
  caret-color:transparent;
}
</style>
